package logic

import (
	"class/classrpc/classrpc"
	"class/classrpc/internal/svc"
	"class/models"
	"context"
	"fmt"

	"github.com/zeromicro/go-zero/core/logx"
)

type CollectAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCollectAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CollectAddLogic {
	return &CollectAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 收藏
func (l *CollectAddLogic) CollectAdd(in *classrpc.CollectAddRequest) (*classrpc.CollectAddResponse, error) {
	// todo: add your logic here and delete this line

	var collect models.Collection
	models.DB.Debug().Where("id = ?", in.CourseId).Find(&collect)
	if collect.ID != 0 {
		return nil, fmt.Errorf("当前课程已收藏")
	}

	collect = models.Collection{
		UserId:   int(in.UserId),
		CourseId: int(in.CourseId),
	}
	models.DB.Debug().Create(&collect)

	return &classrpc.CollectAddResponse{
		CollectId: int64(collect.ID),
	}, nil
}
